<template>
	<view>
		<view v-for="(item,index) in bytesList" :key="index">
			{{item}}
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bytesList: []
			}
		},
		mounted() {
			this.openBluetoothAdapter()
		},
		methods: {
			//蓝牙初始化
			openBluetoothAdapter() {
				uni.openBluetoothAdapter({
					mode: 'peripheral',
					success: async e => {
						console.log('蓝牙初始化');
						// await this.createBLE();
						wx.startBluetoothDevicesDiscovery({
							allowDuplicatesKey: true,
							success: res => {
								this.onBluetoothDeviceFound()
							},
							fail: res => {
								onsole.log("查找设备失败!");
							}
						})

						// wx.getBluetoothDevices({
						//   success: function (res) {
						//     console.log(res)

						//   }
						// })
					},
					fail: e => {
						if (e.errCode == 10001) {
							uni.showModal({
								title: "提示",
								content: "您的蓝牙没有打开，请打开蓝牙",
								success: function(res) {
									if (res.confirm == true) {
										if (uni.getSystemInfoSync().platform == 'ios') {} else if (
											uni.getSystemInfoSync().platform ===
											'android') {}
									} else {
										// uni.navigateBack({
										// 	url: '../../detail3/detail3'
										// })
									}
								},
							});
						}
						throw '初始化蓝牙失败，错误码：'
						console.log('初始化蓝牙失败，错误码：' + (e.errCode || e.errMsg));
					}
				});
			},
			inArray(arr, key, val) {
				for (let i = 0; i < arr.length; i++) {
					if (arr[i][key] === val) {
						return i;
					}
				}
				return -1;
			},
			onBluetoothDeviceFound() {
				wx.onBluetoothDeviceFound((res) => {
					res.devices.forEach(device => {
						/* if (!device.name && !device.localName) {
							return
						}
						const foundDevices = this.data.devices
						const idx = inArray(foundDevices, 'deviceId', device.deviceId)
						const data = {}
						if (idx === -1) {
							data[`devices[${foundDevices.length}]`] = device
						} else {
							data[`devices[${idx}]`] = device
						} */
						var bytes = Array.prototype.map.call(new Uint8Array(device.advertisData), bit => (
							'00' + bit.toString(16)).slice(-2)).join('');
						console.log(bytes,device, '@@')
						this.bytesList = [...this.bytesList, bytes]
					})
				})
			},

		}
	}
</script>

<style>

</style>